/**
 * Created by xie on 2018/3/13.
 */
angular.module('module.mainpage')
    .controller('ArticleController', ArticleController);

ArticleController.$inject = ['$scope', '$state', '$cookies', '$rootScope','$stateParams','MainpageService'];

function ArticleController($scope, $state, $cookies, $rootScope, $stateParams,MainpageService) {

    var vm = this;
    if(!$cookies.get("userInfo")){
       vm.user={
           "userid": 11100000,
           "nickname": "游客",
           "photo": "http://sfs.shuwang.info/sfcommon/1030/2018-04-12/6yc6r5Bqw-lanqiren.jpg",
           "sex": ""
       }
    }
    else{
        vm.user = JSON.parse($cookies.get("userInfo"));
    }

    vm.addLike = addLike;
    vm.heartpng = "heart.png";
    vm.disspng = "diss.png";
    vm.addDiss = addDiss;
    vm.disscolor = "black;";

    vm.addComment = addComment;
    vm.content;
    vm.deleteDiss=deleteDiss;
    var articleid = $stateParams.articleid;


    vm.diss = {
        "articleid": articleid,
        "userid": vm.user.userid,
        "disstime": ""
    }

    vm.comment = {
        "commentid": 0,
        "articleid": articleid,
        "commentedid": 0,
        "userid": vm.user.userid,
        "createtime": "",
        "content": ""
    }

    MainpageService.getArticleById(articleid).$promise.then(function (data) {
        vm.article = data;
        console.log(vm.article);
    }, function (err) {
        console.log(err)
    });

    MainpageService.getCommentByArticleid(articleid).$promise.then(function (data) {
        vm.comments = data;

        console.log(vm.comments);
    }, function (err) {
        console.log(err)
    });



    MainpageService.getDissUser(articleid).$promise.then(function (data) {
        vm.userids = data;
        console.log("userids",vm.userids)
        if(!vm.userids)
        {
            console.log("2222222")
            vm.heartpng = "heart.png";
        }
        else if(vm.userids.indexOf(vm.user.userid) > -1)
        {
            console.log("111111")
            vm.heartpng = "redheart.png"
        }
    }, function (err) {
        console.log(err)
    });

    function addDiss() {
        MainpageService.addDiss(vm.diss).$promise.then(function (data) {
            request = data;
            console.log(request)
            alert("点赞成功");
            window.location.reload();
        }, function (err) {
            console.log(err);
            return alert(err.data.msg);
        });
    }
    function deleteDiss(){

        MainpageService.deleteDissByKey(articleid,vm.user.userid).$promise.then(function (data) {
            request = data;

            console.log(request);
        }, function (err) {
            console.log(err)
        });
    }


       function addComment(){
           MainpageService.addComment(vm.comment).$promise.then(function (data) {
               request = data;
               console.log(request)
               alert("评论成功");
               window.location.reload();
           }, function (err) {
               console.log(err);
               return alert(err.data.msg);
           });
       }


    //function addDiss($index) {
    //
    //    if (vm.disspng == "diss.png") {
    //        vm.disspng = "reddiss.png";
    //        vm.disscolor = "red;";
    //    }
    //
    //    else {
    //        vm.disspng = "diss.png";
    //        vm.disscolor="black;";
    //    }
    //
    //}





    function addLike() {
        if (vm.heartpng == "heart.png")
        {   vm.addDiss();
            vm.heartpng = "redheart.png";
        }
        else{
            vm.deleteDiss();
            vm.heartpng = "heart.png";
        }


    }

    vm.aa = [
        "images/imggg/touxiang6.jpg",
        "images/imggg/touxiang6.jpg",
        "images/imggg/touxiang6.jpg",
        "images/imggg/touxiang6.jpg",
        "images/imggg/touxiang6.jpg",


    ]


}